<template>
  <div>
    <el-table :data="scoreData" style="width: 100%">
      <el-table-column prop="id" label="选课编号" width="200" />
      <el-table-column prop="studentId" label="学生编号" width="200" />
      <el-table-column prop="courseId" label="课程编号" width="200" />
      <el-table-column prop="score" label="分数" width="200" />

      <el-table-column fixed="right" label="操作" width="450">
        <template #default="scoped">
          <el-button type="danger" round @click="deleteScoreDialog(scoped.row.id)"
            >删除</el-button
          >

          <el-button type="warning" round @click="editScoreInfo(scoped.row.id)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页搜索 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 10, 15]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="pageTotal"
      @size-change="pageChange"
      @current-change="currentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </div>

  <!-- 更改学生成绩信息 -->
  <el-dialog v-model="dialogEdit" title="编辑">
    <el-form :model="updateScore">
      <el-form-item label="选课编号">
        <el-input v-model="updateScore.id" disabled />
      </el-form-item>

      <el-form-item label="学生编号">
        <el-input v-model="updateScore.studentId" />
      </el-form-item>
      <el-form-item label="课程编号">
        <el-input v-model="updateScore.courseId" />
      </el-form-item>

      <el-form-item label="分数">
        <el-input v-model="updateScore.score" />
      </el-form-item>
    </el-form>
    <template #footer="scope">
      <span class="dialog-footer">
        <el-button type="warning" round @click="dialogEdit = false">取消</el-button>
        <el-button type="success" round @click="editConfirm"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
import { ElMessageBox } from "element-plus";
import {
  getAllScore,
  deleteScore,
  updateScoreInfo,
  getDetailScoreInfo,
} from "@/api/score.js";

const dialogEdit = ref(false);

const scoreData = ref([]);

const currentPage = ref(1);
const pageSize = ref(3);
const name = ref("");
//总共页数
const pageTotal = ref(0);

const updateScore = ref({
  id: 0,
  studentId: 0,
  courseId: 0,
  score: 0,
});

//页数改变时
const pageChange = async (size) => {
  pageSize.value = size;
  getAllInfo();
};

//页数大小改变时
const currentChange = async (num) => {
  currentPage.value = num;
  console.log(10086);
  console.log(num);
  getAllInfo();
};

//编辑成绩信息
const editScoreInfo = async (id) => {
  dialogEdit.value = true;
  const result = await getDetailScoreInfo(id);
  Object.assign(updateScore.value, result.data);
};

//完成编辑
const editConfirm = async () => {
  let result = await updateScoreInfo(updateScore.value);
  dialogEdit.value = false;
  getAllInfo();
};

//删除成绩信息
const deleteScoreDialog = async (id) => {
  ElMessageBox.confirm("你确认要取消吗?", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      //调用接口
      let result = await deleteScore(id);
      ElMessage({
        type: "success",
        message: "删除成功",
      });
      //刷新列表
      getAllInfo();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除",
      });
    });
};

//获取所有信息
const getAllInfo = async () => {
  let params = {
    page: currentPage.value,
    pageSize: pageSize.value,
  };
  let result = await getAllScore(params);
  console.log(result.data);
  scoreData.value = result.data.records;
  pageTotal.value = result.data.total;
};

onMounted(() => {
  getAllInfo();
});
</script>
